package org.metasketch.ham.model;

import java.util.ArrayList;
import java.util.List;

import org.metasketch.demo.model.CoordinationLink;
import org.metasketch.editor.toolbox.advanced.DeleteRelatedManager;
import org.metasketch.model.BaseModel;
import org.metasketch.model.CommonIdentified;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("ham-model")
public class HAMModel extends BaseModel {

	{
		DeleteRelatedManager.registerDependency(
				HumanActivityParticipationRelationLink.class, "source",
				"target");
		DeleteRelatedManager.registerDependency(
				HumanActivityRelationLink.class, "source", "target");
		DeleteRelatedManager.registerDependency(
				HumanParticipationRelationLink.class, "source", "target");

		DeleteRelatedManager.registerDependency(
				HumanParticipationGeneralizationLink.class, "source", "target");
	}

	@XStreamAlias("human-activity-elements")
	List<HumanActivityElement> humanActivityElements = new ArrayList<HumanActivityElement>();

	@XStreamAlias("human-participation-elements")
	List<HumanParticipationElement> participationElements = new ArrayList<HumanParticipationElement>();

	@XStreamAlias("activity-participation-links")
	List<HumanActivityParticipationRelationLink> activityParticipationLinks = new ArrayList<HumanActivityParticipationRelationLink>();

	public void compileIndex() {
		super.compileIndex();

		for (HumanActivityElement object : humanActivityElements) {
			index.storeNode(object);
		}

		for (HumanParticipationElement object : participationElements) {
			index.storeNode(object);
		}

		for (HumanActivityParticipationRelationLink object : activityParticipationLinks) {
			index.storeNode(object);
		}

	}

	public void addChild(CommonIdentified node) {
		super.addChild(node);

		if (node instanceof HumanActivityElement) {
			humanActivityElements.add((HumanActivityElement) node);
		} else if (node instanceof HumanParticipationElement) {
			participationElements.add((HumanParticipationElement) node);
		} else if (node instanceof HumanActivityParticipationRelationLink) {
			activityParticipationLinks
					.add((HumanActivityParticipationRelationLink) node);
		}

		// index.storeNode(node);

	}

	public void removeChild(CommonIdentified node) {
		super.removeChild(node);

		if (node instanceof HumanActivityElement) {
			humanActivityElements.remove((HumanActivityElement) node);
		} else if (node instanceof HumanParticipationElement) {
			participationElements.remove((HumanParticipationElement) node);
		} else if (node instanceof HumanActivityParticipationRelationLink) {
			activityParticipationLinks
					.remove((HumanActivityParticipationRelationLink) node);
		}

		// index.removeNode(node);

	}

	public List<HumanActivityElement> getHumanActivityElements() {
		return humanActivityElements;
	}

	public void setHumanActivityElements(
			List<HumanActivityElement> humanActivityElements) {
		this.humanActivityElements = humanActivityElements;
	}

	public List<HumanParticipationElement> getParticipationElements() {
		return participationElements;
	}

	public void setParticipationElements(
			List<HumanParticipationElement> participationElements) {
		this.participationElements = participationElements;
	}

	public List<HumanActivityParticipationRelationLink> getActivityParticipationLinks() {
		return activityParticipationLinks;
	}

	public void setActivityParticipationLinks(
			List<HumanActivityParticipationRelationLink> activityParticipationLinks) {
		this.activityParticipationLinks = activityParticipationLinks;
	}

}
